package com.lynch.util;

import com.lynch.bean.ImageSize;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;

/**
 * 图片工具类
 * 
 * @author Lynch
 */
public class ImageUtil {

	/**
	 * 图形缩放，根据bitmap方式 
	 * 
	 * @param bitmap 原图片
	 * @param imageSize 图片的尺寸
	 * @return newBitmap 处理后的图片
	 */
	public Bitmap imageScaleBitmap(Bitmap bitmap, ImageSize imageSize) {
		
		int srcWidth = bitmap.getWidth();
		int srcHeight = bitmap.getHeight();
		/**定义缩放的高和宽的比例*/ 
		float scaleWidthProportion = ((float) imageSize.getWidth()) / srcWidth;
		float scaleHeightProportion = ((float) imageSize.getHeight()) / srcHeight;
		/**创建操作图片的用的Matrix对象*/ 
		Matrix matrix = new Matrix();
		/**缩放图片的比例*/ 
		matrix.postScale(scaleWidthProportion, scaleHeightProportion);
		/**旋转图片角度*/ 
		// matrix.postRotate(rotate);
		Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, imageSize.getWidth(), imageSize.getHeight(), matrix, true);
		bitmap.recycle();
		return newBitmap;
	}
	
	/**
	 * 图形缩放，根据drawable方式
	 * 
	 * @param drawable 原图片
	 * @param imageSize 图片的尺寸
	 * @return newBitmap 处理后的图片
	 */
	public Bitmap imageScaleDrawable(Drawable drawable, ImageSize imageSize){
		
		Bitmap oldbmp = drawableToBitmap(drawable);
	    int scaleWidth = imageSize.getWidth(); 
	    int scaleHeight = imageSize.getHeight();
	    Bitmap newbmp = Bitmap.createScaledBitmap(oldbmp, scaleWidth, scaleHeight, true); 
	    oldbmp.recycle();
	    return newbmp;
	}
	
	/**
	 * 图形缩放，根据屏幕大小自动缩放 
	 * 
	 * @param bitmap 原图片
	 * @param scaleWidth 手机屏幕高度
	 * @return newBitmap 处理后的图片
	 */
	public Bitmap imageScaleScreen(Bitmap bitmap, int scaleWidth){
		
	    int srcWidth = bitmap.getWidth();  
	    int srcHeight = bitmap.getHeight(); 
	    int scaleHeight = (int)((scaleWidth*srcHeight*1.0)/srcWidth);
	    Bitmap newbmp = Bitmap.createScaledBitmap(bitmap, scaleWidth, scaleHeight, true); 
	    bitmap.recycle();
	    return newbmp;
	}
	
	/**
	 * 图形缩放，根据屏幕大小自动缩放 
	 * 
	 * @param drawable 原图片
	 * @param scaleWidth 手机屏幕高度
	 * @return newBitmap 处理后的图片
	 */
	public Bitmap imageScaleScreen(Drawable drawable, int scaleWidth){
		
		Bitmap oldbmp = drawableToBitmap(drawable);
	    int srcWidth = drawable.getIntrinsicWidth();  
	    int srcHeight = drawable.getIntrinsicHeight();  
	    int scaleHeight = (int)((scaleWidth*srcHeight*1.0)/srcWidth);
	    Bitmap newbmp = Bitmap.createScaledBitmap(oldbmp, scaleWidth, scaleHeight, true); 
	    oldbmp.recycle();
	    return newbmp;
	}
	
	/** 
	 * 图片圆角处理 
	 * 
	 * @param bitmap 需要处理的图片
	 * @param roundPX 圆角大小
	 * @return newBitmap 处理后的bitmap
	 */
	public Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPX) {
		
		Bitmap.Config config = Config.ARGB_8888;
		Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), config);
		Canvas canvas = new Canvas(newBitmap);
		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPX, roundPX, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		bitmap.recycle();
		return newBitmap;
	}
	
	/** 
	 * 获得带倒影的图片方
	 *  
	 * @param bitmap 原来的bitmap
	 * @return bitmapWithReflection 带倒影的bitmap
	 */  
	public Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {  
		
	    final int reflectionGap = 4;  
	    int width = bitmap.getWidth();  
	    int height = bitmap.getHeight();  
	    Matrix matrix = new Matrix();  
	    matrix.preScale(1, -1);  
	    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);  
	    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);  
	    Canvas canvas = new Canvas(bitmapWithReflection);  
	    canvas.drawBitmap(bitmap, 0, 0, null);  
	    Paint deafalutPaint = new Paint();  
	    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);  
	    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);  
	    Paint paint = new Paint();  
	    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,  
	            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,  
	            0x00ffffff, TileMode.CLAMP);  
	    paint.setShader(shader);  
	    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));  
	    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()  
	            + reflectionGap, paint);  
	    bitmap.recycle();
	    return bitmapWithReflection;  
	} 
	

	/**
	 * Drawable转Bitmap 
	 * 
	 * @param drawable 需要的drawable
	 * @return bitmap 转换后的bitmap
	 */
	public Bitmap drawableToBitmap(Drawable drawable) {

		int width = drawable.getIntrinsicWidth();
		int height = drawable.getIntrinsicWidth();
		Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
		Bitmap bitmap = Bitmap.createBitmap(width, height, config);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, width, height);
		drawable.draw(canvas);
		return bitmap;
	}
}
